﻿using SD.Infrastructure.EntityBase;
using SD.Toolkits.Recursion.Tree;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlamDunk.ValueObjects.Enums;

namespace SlamDunk.MES.Domain.Entities.FoundationContext
{
    /// <summary>
    /// 工厂模型
    /// </summary>
    public class Facility : AggregateRootEntity, ITree<Facility>
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected Facility()
        {
            //初始化导航属性
            this.SubNodes = new HashSet<Facility>();
        }
        #endregion

        #region 01.创建工厂模型构造器
        /// <summary>
        /// 创建工厂模型构造器
        /// </summary>
        /// <param name="facilityNo">工厂模型编号</param>
        /// <param name="facilityName">工厂模型名称</param>
        /// <param name="facilityLevel">工厂模型层次</param>
        /// <param name="facilityType">工厂模型类型</param>
        /// <param name="description">描述</param>
        /// <param name="parentNode">上级节点</param>
        public Facility(string facilityNo, string facilityName, FacilityLevel facilityLevel, FacilityType? facilityType, string description, Facility parentNode)
            : this()
        {
            base.Number = facilityNo;
            base.Name = facilityName;
            this.FacilityLevel = facilityLevel;
            this.FacilityType = facilityType;
            this.Description = description;
            this.ParentNode = parentNode;
            parentNode?.SubNodes.Add(this);

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #endregion

        #region # 属性

        #region 工厂模型层次 —— FacilityLevel FacilityLevel
        /// <summary>
        /// 工厂模型层次
        /// </summary>
        public FacilityLevel FacilityLevel { get; private set; }
        #endregion

        #region 工厂模型类型 —— FacilityType? FacilityType
        /// <summary>
        /// 工厂模型类型
        /// </summary>
        public FacilityType? FacilityType { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 只读属性 - 是否是根级节点 —— bool IsRoot
        /// <summary>
        /// 只读属性 - 是否是根级节点
        /// </summary>
        public bool IsRoot
        {
            get { return this.ParentNode == null; }
        }
        #endregion

        #region 只读属性 - 是否是叶子级节点 ——  bool IsLeaf
        /// <summary>
        /// 只读属性 - 是否是叶子级节点
        /// </summary>
        public bool IsLeaf
        {
            get { return !this.SubNodes.Any(x => !x.Deleted); }
        }
        #endregion

        #region 导航属性 - 上级节点 —— Facility ParentNode
        /// <summary>
        /// 导航属性 - 上级节点
        /// </summary>
        public virtual Facility ParentNode { get; private set; }
        #endregion

        #region 导航属性 - 下级节点集 —— ICollection<Facility> SubNodes
        /// <summary>
        /// 导航属性 - 下级节点集
        /// </summary>
        public virtual ICollection<Facility> SubNodes { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改工厂模型 —— void UpdateInfo(string facilityName, string description)
        /// <summary>
        /// 修改工厂模型
        /// </summary>
        /// <param name="facilityName">工厂模型名称</param>
        /// <param name="description">描述</param>
        public void UpdateInfo(string facilityName, string description)
        {
            base.Name = facilityName;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #region 设置父级工厂模型 —— void SetParentScope(Facility parentScope)
        /// <summary>
        /// 设置父级工厂模型
        /// </summary>
        /// <param name="parentScope">父级工厂模型</param>
        public void SetParentScope(Facility parentScope)
        {
            this.ParentNode = parentScope;
            parentScope?.SubNodes.Add(this);
        }
        #endregion

        #region 深度获取所有子节点Id列表 —— IList<Guid> GetDeepNodeIds()
        /// <summary>
        /// 深度获取所有子节点Id列表
        /// </summary>
        /// <returns>工厂模型Id列表</returns>
        /// <remarks>包括this.Id</remarks>
        public IList<Guid> GetDeepNodeIds()
        {
            ICollection<Guid> facilityIds = new HashSet<Guid>();

            ICollection<Facility> deepSubNodes = this.GetDeepSubNodes();
            foreach (Facility subNode in deepSubNodes)
            {
                facilityIds.Add(subNode.Id);
            }

            facilityIds.Add(this.Id);

            return facilityIds.ToList();
        }
        #endregion

        #region 初始化关键字 —— void InitKeywords()
        /// <summary>
        /// 初始化关键字
        /// </summary>
        private void InitKeywords()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.Number);
            builder.Append(base.Name);
            builder.Append(this.Description);

            base.SetKeywords(builder.ToString());
        }
        #endregion

        #endregion
    }
}
